package oa.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import oa.entity.Attendance;
import oa.entity.Message;
import oa.entity.Press;
import oa.entity.User;
import oa.exception.DaoException;
import oa.paging.Page;
import oa.service.AttendanceService;
import oa.service.MessageService;
import oa.service.PressService;
import oa.service.UserService;
import oa.service.impl.AttendanceServiceImpl;
import oa.service.impl.MessageServiceImpl;
import oa.service.impl.PressServiceImpl;
import oa.service.impl.UserServiceImpl;

/**
 * Servlet implementation class indexServlet
 */
@WebServlet("/indexServlet")
public class IndexServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
    /**
     * @see HttpServlet#HttpServlet()
     */
    public IndexServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Servlet#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");
		HttpSession session  = request.getSession();		
		PrintWriter out = response.getWriter();
		UserService userService = new UserServiceImpl();
		AttendanceService attendan=new AttendanceServiceImpl();
		MessageService  message = new MessageServiceImpl();
		PressService pressService = new PressServiceImpl();
		Page<Message> pageInfo = new Page<Message>();	
		List<Message> messagelist = new ArrayList<Message>();
		List<Press>  pressList = new ArrayList<Press>();
		String loginopt = session.getAttribute("loginopt").toString();
		int companyid=0;   //获得当前登录的用户公司id
		int userCount = 0; //获得员工数量	
		int userCounts = 0; //签到人数
		int departid = 0;  //获取部门id
		int userid = 0;    //获取到登录的员工id
		int messagecount = 0; //获取通知数量
		String loginname = null; //获取到登录名
		String pageno = request.getParameter("PageNo");
		if(pageno==null) {
			pageno ="1";
		}
		pageInfo.setCurrentPage(Integer.parseInt(pageno)); //设置页数
		pageInfo.setPageSize(5);
		//当前时间
		Date day=new Date();
		SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
	  try {
		if("user".equals(loginopt)) {
			companyid=(int)session.getAttribute("usercompanyid");
			userid=(int)session.getAttribute("userid");
			departid=(int)session.getAttribute("departid");
		    loginname= (String)session.getAttribute("UserLoginName");
		
		}else {
			 companyid=(int)session.getAttribute("logincompanyid");			
			 loginname= (String)session.getAttribute("LoginName");
				
		}
			//员工数量
     		userCount = userService.selectAllUserCount(0,companyid);       	
     		//签到人数
     	    userCounts=attendan.selectCountLists( df.format(day), companyid);	
     	   
     	    /***
		     * @author Administrator
		     * 在这里获取到当前登录公司的通知
		     */
	          if(companyid==0) {
				   messagecount = message.countMessage(0);
				   messagelist = message.selectMessages(0, pageInfo.getCurrentPage(), pageInfo.getPageSize());
			   }else {
				   messagecount = message.countMessage(companyid);
				   messagelist = message.selectMessages(companyid, pageInfo.getCurrentPage(), pageInfo.getPageSize());
			 
			   }
	          pageInfo.setTotalRow(messagecount);//设置总数
	          pageInfo.setPageList(messagelist); //设置集合对象
	  
		     } catch (Exception e) {
				
			}
		request.getSession().setAttribute("competence", session.getAttribute("competence")); //登录的权限
        request.getSession().setAttribute("userCount", userCount); //员工数量
        request.getSession().setAttribute("userCounts", userCounts); //签到人数
		request.getSession().setAttribute("companyid", companyid);  //  公司ID
		request.getSession().setAttribute("userid", userid);     //员工id
		request.getSession().setAttribute("departid",departid);  //部门ID
		request.getSession().setAttribute("LoginName",loginname);  //登录名
		pressList = pressService.selectPress((int)session.getAttribute("competence"));
		request.getSession().setAttribute("pressList", pressList); //权限控制
		request.getSession().setAttribute("PageInfo",pageInfo);
		request.getRequestDispatcher("index.jsp").forward(request, response);
		}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		doGet(request, response);
	}
}
